package com.memo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.memo.asynctasks.DBTask;
import com.memo.controller.CalendarAdapter;
import com.memo.db.DAO;
import com.memo.model.Congratulation;

public class CalendarView extends Activity {

	public static Calendar calendar;
	private CalendarAdapter adapter;
	private Handler handler;
	private ArrayList<String> items; // container to store some random calendar items
	private Activity act;

	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    act = this;
	    setContentView(R.layout.calendar);
	    calendar = Calendar.getInstance();

	    items = new ArrayList<String>();	    

	    handler = new Handler();
	    handler.post(calendarUpdater);
	    
	    String currDate = android.text.format.DateFormat.format("yyyy-MM", calendar.getTime()).toString();
	    
	    TextView title  = (TextView) findViewById(R.id.title);
	    title.setText(currDate);

	    TextView previous  = (TextView) findViewById(R.id.previous);
	    previous.setOnClickListener(new OnClickListener() {
//previous month arrow click
			@Override
			public void onClick(View v) {
				if(calendar.get(Calendar.MONTH)== calendar.getActualMinimum(Calendar.MONTH)) {				
					calendar.set((calendar.get(Calendar.YEAR)-1),calendar.getActualMaximum(Calendar.MONTH),1);
				} else {
					calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
				}
				refreshCalendar();
			}
		});

	    TextView next  = (TextView) findViewById(R.id.next);
	    next.setOnClickListener(new OnClickListener() {
//next month arrow click
			@Override
			public void onClick(View v) {
				if(calendar.get(Calendar.MONTH)== calendar.getActualMaximum(Calendar.MONTH)) {				
					calendar.set((calendar.get(Calendar.YEAR)+1),calendar.getActualMinimum(Calendar.MONTH),1);
				} else {
					calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
				}
				refreshCalendar();

			}
		});
	    
	    adapter = new CalendarAdapter(this, calendar);
	    GridView gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(adapter);
//choosing a day
		gridview.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    	TextView date = (TextView)v.findViewById(R.id.date);
		        if(date instanceof TextView && !date.getText().equals("")) {
		        	
		        	String currDate = ((TextView) findViewById(R.id.title)).getText().toString();
		        	String day = date.getText().toString();
		        	String sDate = currDate + "-" + day;
		        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		        	Date dateToFind = null;
					try {
						   dateToFind = sdf.parse(sDate);

					} catch (java.text.ParseException e) {
					    e.printStackTrace();
					}
//		        	calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
		        	new DBTask(act, dateToFind).execute();
		        }

		    }
		});
	}
//------------------------------------------------------------------------------------------------------
	//refresh calendar title
	public void refreshCalendar()
	{
		TextView title  = (TextView) findViewById(R.id.title);

		adapter.refreshDays();
		adapter.notifyDataSetChanged();				
		handler.post(calendarUpdater); // get values from database				
		title.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH)+1));
	}
//-----------------------------------------------------------------------------------------------------
	//update calendar days with events
	public Runnable calendarUpdater = new Runnable() {

		@Override
		public void run() {
			items.clear();
			List<Congratulation> congs = DAO.getGreatingsByMonth(act, calendar);
			for(Congratulation con : congs){
				items.add(con.getDate().getDate()+"");
			}
			adapter.setItems(items);
			adapter.notifyDataSetChanged();
		}
	};
//------------------------------------------------------------------------------------------------------	
}
